package com.ruicar.afs.cloud.bizcommon.rules.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.bizcommon.dto.rules.atom.AfsRuleCopyDto;
import com.ruicar.afs.cloud.bizcommon.dto.rules.atom.AfsRuleInfoDto;
import com.ruicar.afs.cloud.bizcommon.dto.rules.tree.RuleTreeDto;
import com.ruicar.afs.cloud.bizcommon.rules.entity.AfsRuleInfo;
import com.ruicar.afs.cloud.bizcommon.rules.service.AfsRuleInfoService;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@AllArgsConstructor
@Slf4j
@RestController
@RequestMapping("/afs/rules")
public class AfsRuleController {

    private final AfsRuleInfoService afsRuleInfoService;

    @PostMapping("/saveRule")
    @PreAuthorize("@pms.hasPermission('afs_rule_info_save')")
    @SysLog("保存规则")
    public IResponse<AfsRuleInfoDto> saveRuleInfo(@Validated @RequestBody AfsRuleInfoDto afsRuleInfoDto) {
        return IResponse.success(afsRuleInfoService.checkAndSaveRule(afsRuleInfoDto));
    }

    @PostMapping("/checkRule")
    public IResponse<AfsRuleInfoDto> checkRuleInfo(@Validated @RequestBody AfsRuleInfoDto afsRuleInfoDto) {
        return IResponse.success(afsRuleInfoService.checkRule(afsRuleInfoDto));
    }

    @PostMapping("/queryRule")
    public IResponse<Page<AfsRuleInfo>> queryRule(@RequestBody QueryCondition<AfsRuleInfoDto> queryCondition) {
        LambdaQueryWrapper wrappers = Wrappers
                .<AfsRuleInfo>lambdaQuery()
                .eq(StringUtils.isNotEmpty(queryCondition.getCondition().getRuleNo()), AfsRuleInfo::getRuleNo, queryCondition.getCondition().getRuleNo())
                .like(StringUtils.isNotEmpty(queryCondition.getCondition().getRuleName()), AfsRuleInfo::getRuleName, queryCondition.getCondition().getRuleName())
                .eq(StringUtils.isNotEmpty(queryCondition.getCondition().getRuleGroup()), AfsRuleInfo::getRuleGroup, queryCondition.getCondition().getRuleGroup());
        Page<AfsRuleInfo> page = afsRuleInfoService.page(queryCondition.page(), wrappers);
        return IResponse.success(page);
    }

    @PostMapping("/loadRuleTreeById/{ruleId}")
    public IResponse<List<RuleTreeDto>> loadRuleTree(@PathVariable Long ruleId) {
        return IResponse.success(afsRuleInfoService.loadRuleTree(ruleId));
    }


    @PostMapping("/saveRuleOut")
    public IResponse<Boolean> saveRuleOut(@RequestBody AfsRuleInfoDto afsRuleInfoDto) {
        afsRuleInfoService.saveRuleOut(afsRuleInfoDto);
        return IResponse.success(Boolean.TRUE);
    }

    @PostMapping("/copyRule")
    public IResponse<AfsRuleInfoDto> copyRule(@Validated @RequestBody AfsRuleCopyDto afsRuleCopyDto) {
        return IResponse.success(afsRuleInfoService.copyRule(afsRuleCopyDto).toString());
    }
}
